/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;

import Basicas.Bode;
import java.io.IOException;
import java.util.Timer;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author Marcio
 */
public class NegocioBode {

    public static final String DIREITA = "Direita";
    public static final String ESQUERDA = "Esquerda";
    public static final String CIMA = "Cima";
    public static final String BAIXO = "Baixo";
    private Bode bode;
    private Timer timer;
    private SpriteAnimationTask animator;
    public static final int SPEED = 3;
    private int lastDirection = -1;

    public NegocioBode() {
        try {
            this.bode = new Bode();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.timer = new Timer();

        this.animator = new SpriteAnimationTask(this.bode.getSpriteBode(), false);
        this.timer.scheduleAtFixedRate(this.animator, 0, this.bode.getBodeDelay());
    }

    public void movimentar() {
        this.animator.forward();
    }

    public void andar(String direcao, TiledLayer cenario, TiledLayer colisao) {

        if (direcao.equals(DIREITA)) {
            andarDireita(cenario,colisao);
        } else if (direcao.equals(ESQUERDA)) {
            andarEsquerda(cenario,colisao);
        } else if (direcao.equals(CIMA)) {
            andarCima(cenario,colisao);
        } else if (direcao.equals(BAIXO)) {
            andarBaixo(cenario,colisao);
        } else {
            this.parar();
        }

    }

    public void andarDireita(TiledLayer cenario, TiledLayer colisao) {
        //if the previous direction was other than left set the sequence
        //correct sequence & transform needed for walking to the left
        if (this.lastDirection != GameCanvas.RIGHT) {
            this.lastDirection = GameCanvas.RIGHT;
            this.bode.getSpriteBode().setFrameSequence(bode.getBodeSeqDireita());
            this.bode.getSpriteBode().setTransform(Sprite.TRANS_NONE);
        }
        //move the sprite to the left
        this.movimentar();
        this.bode.getSpriteBode().move(SPEED, 0);
        if (this.colisaoTela(cenario) || this.colisaoCenario(colisao)) {
            this.bode.getSpriteBode().move(-SPEED, 0);
        }
        //if moving the sprite generates a collision return sprite back
        //to its original position
    }

    public void andarEsquerda(TiledLayer cenario, TiledLayer colisao) {
        if (this.lastDirection != GameCanvas.LEFT) {
            this.lastDirection = GameCanvas.LEFT;
            this.bode.getSpriteBode().setFrameSequence(bode.getBodeSeqEsquerda());
            this.bode.getSpriteBode().setTransform(Sprite.TRANS_NONE);
        }
        this.movimentar();
        this.bode.getSpriteBode().move(-SPEED, 0);
        if (this.colisaoTela(cenario) || this.colisaoCenario(colisao)) {
            this.bode.getSpriteBode().move(SPEED, 0);
        }
    }

    public void andarCima(TiledLayer cenario, TiledLayer colisao) {
        if (this.lastDirection != GameCanvas.UP) {
            this.lastDirection = GameCanvas.UP;
            this.bode.getSpriteBode().setFrameSequence(bode.getBodeSeqCostas());
            this.bode.getSpriteBode().setTransform(Sprite.TRANS_NONE);

        }
        this.movimentar();
        this.bode.getSpriteBode().move(0, -SPEED);
         if (this.colisaoTela(cenario) || this.colisaoCenario(colisao)) {
            this.bode.getSpriteBode().move(0, SPEED);
        }
    }

    public void andarBaixo(TiledLayer cenario, TiledLayer colisao) {
        if (this.lastDirection != GameCanvas.DOWN) {
            this.lastDirection = GameCanvas.DOWN;
            this.bode.getSpriteBode().setFrameSequence(bode.getBodeSeqFrente());
            this.bode.getSpriteBode().setTransform(Sprite.TRANS_NONE);

        }
        this.movimentar();
        this.bode.getSpriteBode().move(0, SPEED);
        if (this.colisaoTela(cenario) || this.colisaoCenario(colisao)) {
            this.bode.getSpriteBode().move(0, -SPEED);
        }
    }

    public void parar() {
        this.animator.setMoving(false);
    }

    public Sprite getSpriteBode() {
        return this.bode.getSpriteBode();
    }

    public int getLastDirection() {
        return lastDirection;
    }

    public boolean colisaoTela(TiledLayer cenario) {
        boolean colidiu = false;
        colidiu = this.bode.getSpriteBode().getX() < 0 || this.bode.getSpriteBode().getY() < 0 || this.bode.getSpriteBode().getX() > (cenario.getWidth() - this.bode.getSpriteBode().getWidth()) || this.bode.getSpriteBode().getY() > (cenario.getHeight() - this.bode.getSpriteBode().getHeight());

        return colidiu;
    }

    public boolean colisaoCenario(TiledLayer colisao) {
        return this.bode.getSpriteBode().collidesWith(colisao, true);
    }
}
